$(function(){
  let oldPwd = document.querySelector('input[name=oldPwd]')
  let newPass = document.querySelectorAll('input[name=newPwd]')
  let form = layui.form;
  form.verify({
    newPass: [/^[a-zA-Z0-9_.-]{6,16}$/, '密码必须是6到16位，且不能出现空格和中文'],
    //检验两次密码是否一致
    rePass: function (value) { //value代表确认密码的表单值
      if (value !== newPass[0].value) {
        return '两者密码不一致'
      }
    },
    oldNew:function(value){
      if(value === newPass[0].value){
        return '新密码与旧密码一致'
      }
    }
  });

  // 监听表单事件
  $('#formPwd').on('submit',function(e){
    e.preventDefault();
    $.ajax({
      method:'PATCH',
      url: "/my/updatepwd",
      data: {
        oldPwd:oldPwd.value,
        newPwd:newPass[0].value,
        re_pwd:newPass[1].value
      },
      success: function (res) {
        if(res.code !== 0){
          return layui.layer.msg(res.message)
        }
        layui.layer.msg('密码已更新，请重新登录');
        // location.href = '/login.html'
        // console.log(res);
        $('#formPwd').reset();
      }
    });
  })
})